<?php

include_once 'SPDO.class.php';

class Telephone
{
	protected $numero;
	protected $sonne;
	protected $vole;
	protected $latitude;
	protected $longitude;
	protected $blocage;
	protected $derniereDate;
	protected $suppression;
	protected $localisation;
	protected $parcoursLat;
	protected $parcoursLong;
	protected $parcoursDate;
	protected $dateVole;
	
	public function __construct($m_numero, $m_sonne, $m_vole, $m_latitude, $m_longitude, $m_blocage, $m_date, $m_suppression, $m_dateVole)
	{
		$this->numero = $m_numero;
		$this->sonne = $m_sonne;
		$this->vole = $m_vole;
		$this->latitude = $m_latitude;
		$this->longitude = $m_longitude;
		$this->derniereDate = $m_date;
		$this->suppression = $m_suppression;
		$this->dateVole = $m_dateVole;
		$this->blocage = $m_blocage;	
		
		if($this->vole)
		{
			$this->recupCoord();
		}
	}
	
	public function __get($name)
	{
		if(property_exists(get_class(), $name))
		{
			return $this->$name;
		}
	}
	
	public function volerTelephone($m_vole, $m_blocage, $m_sonne, $m_suppression)
	{
		try 
		{
			$maj = SPDO::pdo()->prepare("UPDATE Telephone SET Vol=:vol, Blocage=:blocage, Sonne=:sonne, Suppression=:suppression, DateVole=NOW() WHERE Numero=:numero");
			$maj->execute(array('vol' => $m_vole,
					'blocage' => $m_blocage,
					'suppression' => $m_suppression,
					'sonne' => $m_sonne,
					'numero' => $this->numero));
			
			$this->vole = $m_vole;
			$this->blocage = $m_blocage;
			$this->sonne = $m_sonne;
			$this->suppression = $m_suppression;
		}
		catch (PDOException $e)
		{
			die($e->getMessage());
		}
	}
	
	public function retrouverTelephone()
	{
		try 
		{
			$maj = SPDO::pdo()->prepare("UPDATE Telephone SET Vol=0, Blocage=0, Sonne=0, Suppression=0, DateVole=0 WHERE Numero=:numero");
			$maj->execute(array('numero' => $this->numero));
			
			$maj = SPDO::pdo()->prepare("DELETE FROM CoordonnesTelephone WHERE Numero=:numero");
			$maj->execute(array('numero' => $this->numero));
		}
		catch (PDOException $e)
		{
			die($e->getMessage());
		}
	}
	
	public function recupCoord()
	{
		$this->parcoursLat = array();
		$this->parcoursLong[] = array();
		$this->parcoursDate[] = array();
		
		$this->parcoursLat[] = $this->latitude;
		$this->parcoursLong[] = $this->longitude;
		$this->parcoursDate[] = $this->derniereDate;
		
		try 
		{
			$recupPos = SPDO::pdo()->prepare("SELECT Latitude, Longitude, Date FROM CoordonnesTelephone WHERE CoordonnesTelephone.Numero=:num ORDER BY Date");
			$recupPos->execute(array("num" => $this->numero));
			
			while ($result = $recupPos->fetch(PDO::FETCH_OBJ))
			{
				$this->parcoursLat[] = $result->Latitude;
				$this->parcoursLong[] = $result->Longitude;
				$this->parcoursDate[] = $result->Date;
			}
		}
		catch (PDOException $e)
		{
			die($e->getMessage());
		}
	}
}